package com.xf.micro.core.order;

import com.xf.micro.core.annotation.Sort;

import java.util.Comparator;
import java.util.List;

/**
 * @author zouxf
 * @date 2023/12/27 10:38
 * @description
 */
public class ComparatorRule implements Comparator {
    static ComparatorRule INSTANCE = new ComparatorRule();

    @Override
    public int compare(Object o1, Object o2) {
        Class<?> o1Class = o1.getClass();
        int sortValue1 = findSortValue(o1Class);
        Class<?> o2Class = o2.getClass();
        int sortValue2 = findSortValue(o2Class);

        return sortValue1 - sortValue2;
    }

    private int findSortValue(Class<?> c) {
        Sort sort1 = c.getAnnotation(Sort.class);
        return sort1.value().getOrder();
    }


    public static void sort(List list) {
        if (list.size()>1) {
            list.sort(INSTANCE);
        }
    }
}
